package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Override
    public PageResponse selectByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingLevelVo> page1= nursingLevelMapper.selectByPage(name, status);
        return PageResponse.of(page1, NursingLevelVo.class);
    }

    @Override
    public List<NursingLevelVo> getAll() {
        List<NursingLevel> projects= nursingLevelMapper.getAll();
        List<NursingLevelVo> projectVos = new ArrayList<>();
        projects.forEach(project -> {
            NursingLevelVo nursingVo = BeanUtil.copyProperties(project, NursingLevelVo.class);
            projectVos.add(nursingVo);
        });


        return projectVos;
    }

    @Override
    public int add(NursingLevelDto project) {
        //重名校验
        if (nursingLevelMapper.getByName(project.getName()) != null) {
            throw new BaseException(BasicEnum. NURSINGLEVEL_INSERT_FAIL_CODE);
        }
        NursingLevel nursing = BeanUtil.copyProperties(project, NursingLevel.class);
        return nursingLevelMapper.add(nursing);
    }

    @Override
    public NursingLevelVo getById(Long id) {
        NursingLevel project= nursingLevelMapper.getById(id);
        NursingLevelVo nursingVo = BeanUtil.copyProperties(project, NursingLevelVo.class);
        return nursingVo;
    }

    @Override
    public int updateById(NursingLevelDto project) {
        NursingLevel nursingLevel = BeanUtil.copyProperties(project, NursingLevel.class);
        return nursingLevelMapper.updateById(nursingLevel);
    }

    @Override
    public int removeById(Long id) {
        return nursingLevelMapper.removeById(id);
    }

    @Override
    public int updateStatus(Long id, Integer status) {
        return nursingLevelMapper.updateStatus(id, status);
    }


}
